package utils;

import android.text.TextUtils;
import android.util.Log;

import java.math.BigDecimal;
import java.sql.Time;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import beans.OperateGoodsDetailsBean;
import beans.TimeoutBean;
import constants.Constants;

/**
 * Created by Administrator on 2018/5/2.
 */

public class FormateUtils<T> {
    public static ArrayList<String> getRfidCodes(String rfidCode) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < rfidCode.length(); i += 28) {
            list.add(rfidCode.substring(i, i + 28));
        }
        return list;
    }

    //获取单片机的地址
    public static String getAddress(byte[] bytes) {
        return DigitalTrans.toHexStringByte(bytes[13]) + DigitalTrans.toHexStringByte(bytes[14]) + DigitalTrans.toHexStringByte(bytes[15]) + DigitalTrans.toHexStringByte(bytes[16]) + DigitalTrans.toHexStringByte(bytes[17]) + DigitalTrans.toHexStringByte(bytes[18]);
    }


    public static String appendZero(String data) {
        if (data.length() == 1) {
            return 0 + data;
        } else {
            return data;
        }
    }

    public static String appendZero(int data) {
        String s = String.valueOf(data);
        if (s.length() == 1) {
            return 0 + s;
        } else {
            return s;
        }
    }

    public static String getOperate(int operateType) {
        if (operateType == 1) {
            return "匹配";
        } else if (operateType == 2) {
            return "验收";
        } else if (operateType == 3 || operateType == 8) {
            return "入柜";
        } else if (operateType == 4 || operateType == 7) {
            return "出柜";
        } else if (operateType == 5) {
            return "使用";
        } else if (operateType == 6) {
            return "退货";
        } else {
            return "";
        }
    }

    public static String[] getRoleType() {
        String[] content = {"护士", "护士长", "高层管理", "供应商", "厂商"};
        return content;
    }

    public static String[] getRoleType2() {
        String[] content = {"护士", "护士长", "高层管理", "供应商"};
        return content;
    }

    public static String getRole(int role) {
        if (role == Constants.ROLE_MANAGE) {
            return "高层管理";
        } else if (role == Constants.ROLE_HEAD_NURSE) {
            return "护士长";
        } else if (role == Constants.ROLE_NURSE) {
            return "护士";
        } else if (role == Constants.ROLE_FACTORY) {
            return "厂商";
        } else {
            return "供应商";
        }

    }

    public static List getDistinctList(List<OperateGoodsDetailsBean.DataBean> list, int cabinetCode) {
        Map<String, OperateGoodsDetailsBean.DataBean> map = new HashMap();
        OperateGoodsDetailsBean.DataBean bean;
        String gid;
        List newList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            bean = (OperateGoodsDetailsBean.DataBean)list.get(i);
            gid = bean.getGID();
            if (map.get(gid) != null) {
                //map里面已经有了,进行数量的叠加
                OperateGoodsDetailsBean.DataBean dataBean = map.get(gid);
                bean.setNumber(dataBean.getNumber() + 1);
                bean.getBarCodeList().addAll(dataBean.getBarCodeList());
            }
            String time;
            if (bean.getReturnTime() == null){
                time = TimeUtil.getMillisecondDate();
            }else {
                time = bean.getReturnTime().replace("T"," ");
            }
            bean.getBarCodeList().add(bean.getBarCode() + Constants.INCREASE_DECREASE_SPLIT + time);
            bean.setSelectCabinet(String.valueOf(cabinetCode));
            map.put(gid, bean);
        }
        Set set = map.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry mapentry = (Map.Entry) iterator.next();
//            String goodsInfoId = (String) mapentry.getKey();
            newList.add(mapentry.getValue());

        }
        return newList;
    }

    public static List getInventoryDistinctList(List<OperateGoodsDetailsBean.DataBean> list) {
        Map<String, OperateGoodsDetailsBean.DataBean> map = new HashMap();
        OperateGoodsDetailsBean.DataBean bean;
        String gid;
        List newList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            bean = list.get(i);
            gid = bean.getGID()+ bean.getLogicNumber();
            if (map.get(gid) != null) {
                //map里面已经有了,进行数量的叠加
                OperateGoodsDetailsBean.DataBean dataBean = map.get(gid);
                bean.setNumber(dataBean.getNumber() + 1);
                bean.getBarCodeList().addAll(dataBean.getBarCodeList());
            }
            String time;
            if (bean.getReturnTime() == null) {
                 time = TimeUtil.getMillisecondDate();
            }else {
                time = bean.getReturnTime().replace("T"," ");
            }
            bean.getBarCodeList().add(bean.getBarCode() + Constants.INCREASE_DECREASE_SPLIT + time);
            map.put(gid, bean);
        }
        Set set = map.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry mapentry = (Map.Entry) iterator.next();
//            String goodsInfoId = (String) mapentry.getKey();
            newList.add(mapentry.getValue());

        }
        return newList;
    }

    public static float div(int v1, int v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).floatValue();
    }

}
